package com.singfan.script;

import com.singfan.core.LoggerFactory;
import com.singfan.core.dao.database.ICustomerCouponDao;
import com.singfan.core.dao.database.IShopHairAppointmentDetailDao;
import com.singfan.core.dao.database.IShopHairAppointmentSummaryDao;
import com.singfan.core.dao.database.model.ShopHairAppointmentDetail;
import com.singfan.core.dao.database.model.constant.OrderStatusType;
import com.singfan.core.service.IBarberService;
import com.singfan.core.service.IOrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
/**
 * 判断订单过期未支付的脚本类
 * Author: hansonghe
 * Date: 2015-09-02 17:41
 * Desc: 目前此脚本作为一个后台进程，每一分钟执行一次。如果订单数量较多，可以适当延长轮询时间，或者改为消息队列形式。
 */
@Component
public class OrderExpiredScript extends ScriptRunner {
    /**
     * 订单逾期未支付的时间，目前为3小时
     */
    private static final int ExpiredTime = 3 * 60 * 60 * 1000;
    private Logger orderExpiredLogger = LoggerFactory.getOrderExpiredLogger();
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IBarberService barberService;
    @Autowired
    private IShopHairAppointmentSummaryDao shopHairAppointmentSummaryDao;
    @Autowired
    private IShopHairAppointmentDetailDao shopHairAppointmentDetailDao;
    @Autowired
    private ICustomerCouponDao customerCouponDao;

    @Override
    public void run(List<String> args) {
        while (true){
            try {
                long limitTime = System.currentTimeMillis() - ExpiredTime;
                Date limitCreateTime = new Date(limitTime);
                //逾期未支付的订单Id列表
                List<Long> orderIdList = orderService.findExpiredOrder(limitCreateTime);
                int orderCount = orderIdList.size();
                String orderIdStr = "[";
                for (Long orderId : orderIdList){
                    orderIdStr += orderId + ",";
                    //更改订单的状态为逾期未支付
                    orderService.updateOrderStatus(orderId, OrderStatusType.PAY_EXPIRED);
                    //释放技师被预约的时间段
                    barberService.deleteAppointment(orderId);
                    //返还用户使用的优惠券
                    customerCouponDao.cancelOrderId(orderId);
                    //释放门店被预约的时间段的资源
                    ShopHairAppointmentDetail shopHairAppointmentDetail = shopHairAppointmentDetailDao.find(orderId);
                    if (shopHairAppointmentDetail != null){
                        long shopId = shopHairAppointmentDetail.getShopId();
                        int appointmentDay = shopHairAppointmentDetail.getAppointmentDay();
                        int appointmentClock = shopHairAppointmentDetail.getAppointmentClock();
                        shopHairAppointmentDetailDao.delete(orderId);

                        shopHairAppointmentSummaryDao.reduceAppointmentCount(shopId, appointmentDay, appointmentClock);
                    }
                }
                orderIdStr += "]";

                orderExpiredLogger.debug(String.format("%1$d orders expired %2$s !", orderCount, orderIdStr));

                Thread.currentThread().sleep(60 * 1000);
            }
            catch (Exception e) {
                e.printStackTrace();
                orderExpiredLogger.debug(String.format("OrderExpiredScript:run() error -> %1$s !", e.getMessage()));
            }
        }
    }
}
